Strategy reporting and tracking¶

Let's inspect and visualize the input to our strategy and its results.

  • Plot data of market orders and distances from mid.
  • Plot quoted orders and best ask and bid.
  • Register filled trades in plot.
  • Look at the fill ratio.
In [1]:
import pandas as pd
import json
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
In [2]:
quotes = pd.read_json("quotes.json")
In [3]:
with open("order-depths.json", "r") as fp:
    depths = json.load(fp)
    depth = [int(k) for k in depths.keys()]
    counts = depths.values()
In [4]:
d= {"depth_cents": depth, "count": counts}
depth_df = pd.DataFrame(d)
depth_df.head()
Out[4]:
depth_cents count
0 0 221
1 155 1
2 -155 2
3 -98 1
4 29 1
In [5]:
px.histogram(depth_df, x="depth_cents", y="count")

We decide to place our bid and asks as these distances from the best ask and bid.

  • Q1 quantile represents the spread for the bid
  • Q3 quantile represents the spread for the ask
In [6]:
depth_df["depth_cents"].quantile(0.25)
Out[6]:
-100.0
In [7]:
depth_df["depth_cents"].quantile(0.75)
Out[7]:
52.75

This is what happened uring the simulation.

In [8]:
quotes
Out[8]:
time best_bid best_ask my_bid my_ask filled_asks filled_bids
0 1.643832e+09 37614.99 37615.00 37613.99 37615.53 0 0
1 1.643832e+09 37602.80 37602.81 37601.80 37603.34 0 1
2 1.643832e+09 37605.99 37606.00 37604.99 37606.53 1 0
3 1.643832e+09 37579.58 37579.59 37578.58 37580.12 0 1
4 1.643832e+09 37579.58 37579.59 37578.58 37580.12 1 0
5 1.643832e+09 37552.76 37552.77 37551.76 37553.30 0 1
6 1.643832e+09 37551.02 37551.03 37550.02 37551.56 1 1
7 1.643832e+09 37567.43 37567.93 37566.43 37568.46 1 1
8 1.643832e+09 37573.00 37573.01 37572.00 37573.54 1 1
9 1.643832e+09 37590.77 37590.78 37589.77 37591.31 1 0
10 1.643832e+09 37594.74 37594.75 37593.74 37595.28 1 1
11 1.643832e+09 37591.43 37591.44 37590.43 37591.97 1 1
12 1.643832e+09 37584.00 37584.01 37583.00 37584.54 1 1
13 1.643832e+09 37577.97 37577.98 37576.97 37578.51 0 1
14 1.643832e+09 37588.36 37590.80 37587.36 37591.33 1 0
15 1.643832e+09 37600.71 37600.72 37599.71 37601.25 1 1
16 1.643832e+09 37602.44 37602.45 37601.44 37602.98 1 1

Visualize the data¶

All filled orders are marked as dots on the plot.

In [9]:
quotes["time_rel"] = quotes["time"] - quotes["time"][0]

fig1 = px.line(quotes, x="time_rel", y=["best_ask","best_bid","my_ask","my_bid"])

fig2 = px.scatter(quotes[quotes["filled_asks"] == 1], x="time_rel", y="my_ask")
fig3 = px.scatter(quotes[quotes["filled_bids"] == 1], x="time_rel", y="my_bid")
fig4 = go.Figure(data=fig1.data + fig2.data + fig3.data)
fig4.show()

Get the fill ratio of bids and asks¶

In [10]:
quotes["filled_asks"].mean()
Out[10]:
0.7058823529411765
In [13]:
quotes["filled_bids"].mean()
Out[13]:
0.7058823529411765
In [ ]:
 
In [ ]: